﻿using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using ProtetoresDaMata.Data;
using System.Linq;

namespace ProtetoresDaMata
{
    public partial class RankingPostPage : PhoneApplicationPage
    {
        public RankingPostPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadScore();
        }

        private void LoadScore()
        {
            txbDate.Text = DateTime.Now.ToShortDateString();
            txbDistScore.Text = VerifyQueryString("DistanceScore");
            txbIconsScore.Text = VerifyQueryString("IconsScore");
            txName.Text = DeviceNetworkInformation.CellularMobileOperator;
        }

        private void ScorePersist()
        {
            using (GameDataContext context = new GameDataContext())
            {
                Score score = new Score();
                score.Date = Convert.ToDateTime(txbDate.Text);
                score.PointsScore = Convert.ToInt32(txbIconsScore.Text);
                score.DistanceScore = Convert.ToInt32(txbDistScore.Text);

                if (String.IsNullOrEmpty(txName.Text))
                {
                    score.PlayerName = DeviceNetworkInformation.CellularMobileOperator;
                }
                else
                {
                    score.PlayerName = txName.Text;
                }


                context.GetTable<Score>().InsertOnSubmit(score);
                context.SubmitChanges();

                context.Scores.ToList().Remove(context.Scores.ToList().OrderByDescending(obj => obj.DistanceScore + obj.PointsScore).First());

                Score scoreAccumulated = context.Scores.Where(obj => obj.PlayerName == "PointsAccumulated").First();

                if (scoreAccumulated != null)
                {
                    scoreAccumulated.PointsScore = scoreAccumulated.PointsScore + score.PointsScore;
                    scoreAccumulated.DistanceScore = scoreAccumulated.DistanceScore + score.DistanceScore;

                    context.SubmitChanges();
                }
            }


        }


        private string VerifyQueryString(string key)
        {
            if (String.IsNullOrEmpty(NavigationContext.QueryString[key]))
            {
                return "0";
            }
            else
            {
                return NavigationContext.QueryString[key];
            }

        }


        private void Play_Click(object sender, RoutedEventArgs e)
        {
            ScorePersist();
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            ScorePersist();
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

        }


    }
}